package com.yneusoft.manage.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yneusoft.dao.entity.User;
import com.yneusoft.dao.entity.UserState;
import com.yneusoft.dao.mapper.UserMapper;
import com.yneusoft.manage.model.param.ChangeUserStateParam;
import com.yneusoft.manage.model.param.SaveUserParam;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * @author lt
 * @date 2021/10/25 19:18
 * @description: userManage切面
 */


@Aspect
@Component
public class UserManageAspect {

    @Autowired
    private UserMapper userMapper;

    @Pointcut(value = "execution(public * com.yneusoft.manage.controller.UserManageController.saveUser(..))")
    public void saveUser(){}

    @Before("saveUser()")
    public void getStateName(JoinPoint joinPoint){
        Object[] list = joinPoint.getArgs();
        for(Object obj :list){
            if(obj instanceof SaveUserParam){
                SaveUserParam param = (SaveUserParam)obj;
                UserState userState = UserState.dataMap.get(1);
                param.setStateId(userState.getId());
                param.setStateName(userState.getName());
            }
        }

    }

    @Pointcut(value = "execution(public * com.yneusoft.manage.controller.UserManageController.changeState(..))")
    public void changeState(){}

    @Before("changeState()")
    public void changeStateName(JoinPoint joinPoint){
        Object[] list = joinPoint.getArgs();
        for(Object obj :list){
            if(obj instanceof ChangeUserStateParam){
                ChangeUserStateParam param = (ChangeUserStateParam)obj;
                LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(User::getAid,param.getAid());
                User user = userMapper.selectOne(wrapper);
                UserState userState ;
                if(user.getStateId() ==0){
                    userState = UserState.dataMap.get(1);
                    param.setStateId(userState.getId());
                    param.setStateName(userState.getName());
                }
                else{
                    userState = UserState.dataMap.get(0);
                    param.setStateId(userState.getId());
                    param.setStateName(userState.getName());
                }
            }
        }
    }

}
